package com.lh.music.activity;

import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bumptech.glide.Glide;
import com.lh.music.R;
import com.lh.music.adapter.NetSongAdapter;
import com.lh.music.api.Api;
import com.lh.music.callback.CallBack;
import com.lh.music.constant.Constants;
import com.lh.music.entity.Bitrate;
import com.lh.music.entity.NetSong;
import com.lh.music.service.PlayNetService;
import com.lh.music.util.RequestUtil;

import java.util.ArrayList;
import java.util.List;

public class PaihanDetailActivity extends BaseActivity implements CallBack{
    private ListView listView;
    private List<NetSong> mData=new ArrayList<>();
    private List<Bitrate> bitrateList=new ArrayList<>();
    private RequestUtil requestUtil;
    private NetSongAdapter adapter;
    private String type="";
    private View headerView;
    int songPosition=-1;

    MediaPlayer player;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_paihan_detail);
        initHeaderView();
        initView();
        initData();
    }

    private void initHeaderView() {
        headerView=getLayoutInflater().inflate(R.layout.headerview_paihang_detail,null);
    }

    private void initView() {
        listView= (ListView) findViewById(R.id.listView);
        type=getIntent().getStringExtra("type");
        requestUtil=new RequestUtil(this,this);
    }

    private void initData() {
        requestUtil.getPaihangContent(Api.PAIHANG_DETAIL_API+type);
    }

    @Override
    public void callBack(String json) {

        JSONObject job= JSON.parseObject(json);
        JSONArray array=job.getJSONArray("song_list");
        mData.addAll(JSON.parseArray(array.toJSONString(),NetSong.class));
        //初始化headerview的数据
        JSONObject billboard=job.getJSONObject("billboard");
        ImageView icon= (ImageView) headerView.findViewById(R.id.header_icon);
        TextView date= (TextView) headerView.findViewById(R.id.update_date);
        Glide.with(PaihanDetailActivity.this)
                .load(billboard.getString("pic_s444"))
                .placeholder(R.mipmap.a9d)
                .crossFade()
                .centerCrop()
                .into(icon);
        date.setText("更新日期："+billboard.getString("update_date"));
       /* for (int i=0;i<mData.size();i++){
            //发起okhttp同步get请求  获得歌曲的播放地址
            //1 创建okhttpclient对象
            OkHttpClient client=new OkHttpClient();
            //2  创建请求对象
            Request request=new Request.Builder().url(Api.SONG_PLAY_API+mData.get(i).getSong_id()).build();
            //3 创建call对象
            Call call=client.newCall(request);
            //发起同步get请求
            Response mResponse= null;
            try {
                mResponse = call.execute();
                if (mResponse.isSuccessful()) {
                    String s=mResponse.body().toString();
                } else {
                    throw new IOException("Unexpected code " + mResponse);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }*/
        adapter=new NetSongAdapter(PaihanDetailActivity.this,mData);
        listView.addHeaderView(headerView);
        listView.setAdapter(adapter);
        adapter.notifyDataSetChanged();
        final Intent netService=new Intent(PaihanDetailActivity.this, PlayNetService.class);
        startService(netService);
        //对listview进行监听
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                //发送play广播  更换当前播放列表
                app.currentNetSongList.clear();
                app.currentNetSongList.addAll(mData);
                app.currentPosition=position-1;
                Intent broadIntent=new Intent();
                broadIntent.setAction(Constants.NET_PLAY_ACTION);
                sendBroadcast(broadIntent);
            }
        });

        }


    @Override
    public void errorCallBack() {

    }

    @Override
    public View getChildContentView() {
        return getLayoutInflater().inflate(R.layout.activity_paihan_detail,null);
    }

}
